import { ref } from "vue";
import { goToPage, showModal } from "../../../utils/utils";
import { onReachBottom, onShow } from "@dcloudio/uni-app";
import { myAccountOverviewStore } from "../../store";
import { storeToRefs } from "pinia";
import { db_riderGetOrderDetails } from "../../db/order-details";
import { objectAny } from "../../../options/base";
import { orderStatusToggle, tableModuleToggle } from "../../utils/status-toggle";


export default {
    setup() {
        const accountOverviewStore=myAccountOverviewStore();
         // 用户基本信息包括仪表盘数据
        const {accountOverview } = storeToRefs(accountOverviewStore);
        const loadMoreConfig=ref<{
            pageSize:number;
            status:`more`|`loading`|`noMore`|`-1`;
            lastViewDate:number;
            text:objectAny
        }>({
            pageSize:5,
            status:`-1`,        //默认没有加载
            lastViewDate:+new Date(),
            text:{
                contentdown: '上拉加载更多数据',
                contentrefresh: '正在加载，请稍后...',
                contentnomore: '----- 暂无更多订单数据 -----'
            }
        })
        // 今日订单详细列表数据
        const toDayOrderList = ref<Array<objectAny>>([])  
        onReachBottom(()=>{
            getMyAccountOverview();
        })
        onShow(async ()=>{
            accountOverviewStore.getMyAccountOverview();
            getMyAccountOverview();
        })
        // 开始获取列表数据
        async function getMyAccountOverview(){
            if(loadMoreConfig.value.status==='loading'){
                return console.warn(`======> getMyAccountOverview 正在获取数据，无需多次获取，本次获取取消<======`)
            }
            loadMoreConfig.value.status='loading';
            loadMoreConfig.value.lastViewDate = +new Date();

            let pageSize=loadMoreConfig.value.pageSize;
            if(toDayOrderList.value.length >= pageSize){
                loadMoreConfig.value.pageSize+=5;
                pageSize=loadMoreConfig.value.pageSize
            }
            try {
                const {data,myErrCode,errMsg} =await db_riderGetOrderDetails({
                    pageSize:loadMoreConfig.value.pageSize,
                    status:3,
                    justToDay:true
                });
                loadMoreConfig.value.status='more';
                if(myErrCode !== 0){
                    loadMoreConfig.value.pageSize-=5;
                    return showModal({
                        title:`异常提示`,
                        content:errMsg,
                        showCancel:false
                    }) 
                }

                const listData = data?.list as Array<objectAny>;
                if(pageSize > listData.length){ //无更多数据
                    loadMoreConfig.value.status='noMore';
                }
                const sortList = listData.sort((a,b)=>a.order_end_time - b.order_end_time)

                toDayOrderList.value=sortList
            } catch (error) {
                loadMoreConfig.value.status='more';
                console.error(`xxxxxx=> 获取 getMyAccountOverview 列表错误，错误信息如下： <=xxxxxx`)
                console.error(error)
                return showModal({
                    title:`异常提示`,
                    content:`访问过于火爆，请稍后再试`,
                    showCancel:false
                }) 
            }
        }

        function goToOtherPage(path:string,params:objectAny|undefined={}){
            goToPage({
                path,
                navType: `push`,
                params
            });
        }

        return {
            tableModuleToggle,
            orderStatusToggle,
            loadMoreConfig,
            accountOverview,
            toDayOrderList,
            goToOtherPage
        }
    }
}